<?php

namespace front\modules\app\controllers;

use front\controllers\WebBaseController;
use Yii;
use front\models\ProductModel;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\helpers\GoodsHelper;
use yii\helpers\ArrayHelper;
use goods\services\ProductService;
use goods\services\CollectionService;
use backfeed\services\CommentService;
use coupons\services\CouponsGroupService;
/**
 * ProductController implements the CRUD actions for ProductModel model.
 */
class ProductController extends WebBaseController
{
    private $pid;
    public $enableCsrfValidation = false;
    function init(){
        parent::init();
        $this->pid = Yii::$app->request->get('pid') ? Yii::$app->request->get('pid') : '' ;

    }
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }


    /**
     * Lists all ProductModel models.
     * @return mixed
     */
    public function actionDetail()
    {
        if(!$this->pid){
            WebBaseController::getAjaxData(2,'商品不存在');
        }
        //获取商品详细信息
        $goodsDetail = ProductService::ProductInfo($this->pid);
        $arr['code'] = $goodsDetail['code'];
        $arr['message'] = $goodsDetail['message'];
        $arr['product'] = ArrayHelper::toArray($goodsDetail['result']['productInfo']);
        $arr['productInfo']['created_at'] = date('Y-m-d H:i:s');
        $arr['productInfo']['updated_at'] = date('Y-m-d H:i:s');
        $arr['productInfo']['collect'] = CollectionService::getCollection(['P_Id'=>$arr['product']['pid']]);  //商品收藏数
        $arr['productInfo']['collect']['result'] =ArrayHelper::toArray($arr['productInfo']['collect']['result']);
        $arr['productInfo']['collect']['result']['count'] =count(ArrayHelper::toArray($arr['productInfo']['collect']['result']));
        $arr['productInfo']['comment'] = CommentService::search(['product_id'=>$arr['product']['pid']]);  //商品评论数
        $arr['productInfo']['comment']['result'] = ArrayHelper::toArray($arr['productInfo']['comment']['result']);
        $arr['productInfo']['comment']['result']['count'] =count(ArrayHelper::toArray($arr['productInfo']['comment']['result']));
        $arr['productDetail'] = ArrayHelper::toArray($goodsDetail['result']['productDetail']);
        $arr['product']['promotion'] = $this->getPromotr($arr['product']['promotion']);
        $arr['product']['promotion'] = ArrayHelper::toArray($arr['product']['promotion']);
        if($arr['code']==0){
            WebBaseController::getAjaxData(1,$arr['message'],$arr);
        }
    }

    /**
     * 获取对应的卡券信息
     */

    function getPromotr($data){
        foreach($data as $k=>$v){
            $res[] =CouponsGroupService::getGroupInfo($v);
        }
        return $res;
    }

    /**
     * 修改商品，上架，下架的
     */
    function actionChangestatus(){
        $pid = Yii::$app->request->post('pid');
        $status = Yii::$app->request->post('status');
        if(!$pid){
            WebBaseController::getAjaxData(2,'商品不存在');
        }
        if(empty($status)){
            WebBaseController::getAjaxData(3,'请选择修改状态');
        }
        if(ProductService::changeStatus($pid,$status)){
            WebBaseController::getAjaxData(4,'修改成功');
        }


    }

    /**
     * 更新商品信息
     */
    function actionUpdateinfo(){
        $updateInfo = Yii::$app->request->post();
        if(!$updateInfo['pid']){
            WebBaseController::getAjaxData(2,'商品不存在');
        }
        if(ProductService::updateProductInfo($updateInfo)){
            WebBaseController::getAjaxData(3,'修改成功');
        }
    }

    /**
     * 获取热销类目
     */
    function actionHotCategory()
    {
        $shop_id = Yii::$app->request->get();
        if (empty($shop_id)) {
            WebBaseController::getAjaxData(3,'未传递shop_id参数');
        }
        $category = ProductService::hotCategory($shop_id);
        if (empty($category)) {
            WebBaseController::getAjaxData(2,'未查询到店铺对应的类目');
        }
        WebBaseController::getAjaxData(1,$category);
    }

    /**
     * 获取指定类目下商品列表
     * @return array
     */
    public function actionList()
    {
        $category_id = Yii::$app->request->post();
        if (empty($category_id)) {
            WebBaseController::getAjaxData(3,'未传递category_id参数');
        }
        $list = ProductService::productList($category_id);
        if (empty($list)) {
            WebBaseController::getAjaxData(2,'未查询到类目对应的商品信息');
        }
        WebBaseController::getAjaxData(1,$list);
    }

    /**
     * 获取各种类型促销商品列表
     * @return array
     */
    public function actionPromotion()
    {
        $shop_id = Yii::$app->request->post('shop_id');
        $type = Yii::$app->request->post('type');
        if (empty($shop_id)) {
            WebBaseController::getAjaxData(3,'未传递shop_id参数');
        }
        if (($type != 0) && (empty($type))) {
            WebBaseController::getAjaxData(4,'未传递type参数');
        }
        return ProductService::promotionList($shop_id,$type);
    }


    /**
     * Displays a single ProductModel model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ProductModel model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ProductModel();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->pid]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing ProductModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->pid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ProductModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ProductModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ProductModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ProductModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
